package com.example.su_shi_tan_mine.child

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.adapter.JssBaseViewHolder
import com.example.su_shi_tan_bas_lib.base.SimpleListFragment
import com.example.su_shi_tan_bas_lib.bean.CommonBean
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_bas_lib.utils.even.MessageWrap
import com.example.su_shi_tan_core.arouter.DELIVERY_INFO_HISTORY_BEAN
import com.example.su_shi_tan_core.arouter.MineARouter
import com.example.su_shi_tan_core.bean.AddDeliveryInfoHistoryBean
import com.example.su_shi_tan_core.extentions.startNavigation
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.arouter.SubPageARouter
import ikidou.reflect.TypeBuilder
import org.greenrobot.eventbus.EventBus
import java.lang.reflect.Type

@Route(path = MineARouter.ADD_DELIVERY_INFO_HISTORY)
class AddDeliveryInfoHistoryFragment : SimpleListFragment<AddDeliveryInfoHistoryBean>(),
    OnItemChildClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        delayedTime = 0
        isEnableLoadMore = false
        isAddDividerItemDecoration = false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        mHeader.setBackgroundResource(R.color.bg_color_0)
        addHeader(R.layout.add_delivery_info_header_history)

        mHeader.findViewById<View>(R.id.addAddressTv)
            .setOnClickListener {
                ARouter.getInstance()
                    .build(MineARouter.ADD_DELIVERY_INFO)
                    .startNavigation(this)
            }


        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "收货地址"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mAdapter.addChildClickViewIds(R.id.useIt, R.id.editor, R.id.deleteTv)
        mAdapter.setOnItemChildClickListener(this)
    }

    override fun getItemLayout(): Int {
        return R.layout.add_delivery_info_history_fragment

    }

    override fun convertItem(helper: JssBaseViewHolder?, item: AddDeliveryInfoHistoryBean?) {
        helper?.setText(R.id.tile_tv, "${item?.recName()}-${item?.recPhone()}")
            ?.setText(R.id.tile_tv2, "${item?.recAddr()}")
            ?.setViewSelect(R.id.defaultAv, item?.isDefault() == true)
            ?.setViewVisible(R.id.useIt, item?.isDefault() == false)
            ?.setViewVisible(R.id.okIcon, item?.isDefault() == true)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(AddDeliveryInfoHistoryBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        RestClient.builder()
            .url(R.string.address_list)
            .params("page", pageIndex)
            .params("limit", 20)
            .success {
                parseDate(it)
            }
            .error { _, _ ->
                onFailed()
            }
            .failure {
                onFailed()
            }
            .build().get()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val mArray = JSON.parseArray(data, AddDeliveryInfoHistoryBean::class.java)
        loadListDate(mArray)

    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
//        R.id.useIt,R.id.editor,R.id.deleteTv
        when (view.id) {
            R.id.useIt -> {
                useItem(item)
            }
            R.id.editor -> {
                ARouter.getInstance()
                    .build(MineARouter.ADD_DELIVERY_INFO)
                    .withSerializable(DELIVERY_INFO_HISTORY_BEAN, item)
                    .startNavigation(this)
            }
            R.id.deleteTv -> {

                val maps = mapOf(
                    "id" to item?.id,
                )

                RestClient.builder()
                    .url(R.string.deleteShopAddr)
                    .raw(JSON.toJSONString(maps))
                    .success {
                        mAdapter.removeAt(position)

                    }.failure {
                        normalShortToast(_mActivity, R.string.sending_failed)

                    }.error { _, msg ->
                        normalShortToast(_mActivity, msg)

                    }
                    .build()
                    .post()
            }
        }
    }

    private fun useItem(item: AddDeliveryInfoHistoryBean?) {
        val maps = mapOf(
            "id" to item?.id
        )
        RestClient.builder()
            .url(R.string.updateShopAddr)
            .raw(JSON.toJSONString(maps))
            .success {

                EventBus.getDefault().post(MessageWrap.getInstance(MineARouter.ADD_DELIVERY_INFO))

            }.failure {
                Toast.makeText(_mActivity, R.string.sending_failed, Toast.LENGTH_SHORT)
                    .show()
            }.error { _, msg ->
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show()
            }
            .build()
            .post()
    }
}